package com.game77.guessTheWords2.core;

import java.util.HashMap;

import com.game77.guessTheWords2.entity.MapInfo;
import com.game77.guessTheWords2.entity.StageData;
import com.game77.guessTheWords2.util.Ccc;
import com.game77.guessTheWords2.util.GsonHelper;
import com.game77.guessTheWords2.util.ShareSettingUtil;
import com.google.gson.reflect.TypeToken;

public class StageManager {
	public static StageManager instance = new StageManager();

	private StageManager() {
	}

	private static final String LAST_STAGE_KEY = "lastStageKey";
	private static final String LAST_GAME_CLASS_KEY = "lastGameClassKey";
	private static final String LAMP_COUNT_KEY = "iyoye9820937980132";
	private static final String GOLD_COUNT_KEY = "gliujsldkfjo21389";
	private static final String CLASS_UNLOCK_KEY = "liulkjsdvbnakuylertngs";
	private static final String LAST_CLASS_STAGE_INDEX = "LAST_CLASS_STAGE_INDEX";
	private static final String IS_AUDIO_OPEN_KEY = "IsAudioOpenKey";

	// 去广告道具相关
	private static final String ISBUY_NOAD = "isbuyNoadkey";

	// 每个关卡的Key前缀
	private static final String STATE_DATA_ONE_KEYPRE = "OneStage";

	private Object stageDatasSyncLock = new Object();
	private HashMap<String, StageData> stageDatas;
	public HashMap<String, MapInfo> mapInfos;
	private int lampCount;
	private int lastStage;
	private String goldCount;
	private int lastClass;
	private int lastGameClass;
	private boolean isBuyNoAD;
	private boolean isAudioOn;

	public boolean isAudioOn() {
		return isAudioOn;
	}

	public void setAudioOn(boolean isAudioOn) {
		this.isAudioOn = isAudioOn;
		ShareSettingUtil.storeBooleanSetting(IS_AUDIO_OPEN_KEY, isAudioOn);
	}

	public int getLastGameClass() {
		return lastGameClass;
	}

	public void setLastGameClass(int lastGameClass) {
		this.lastGameClass = lastGameClass;
		ShareSettingUtil.storeIntSetting(LAST_GAME_CLASS_KEY, lastGameClass);
	}

	public int getLastClass() {
		return lastClass;
	}

	public void setLastClass(int lastClass) {
		this.lastClass = lastClass;
	}

	public boolean isBuyNoAD() {
		return isBuyNoAD;
	}

	public void setBuyNoAD(boolean isBuyNoAD) {
		this.isBuyNoAD = isBuyNoAD;
		ShareSettingUtil.storeBooleanSetting(ISBUY_NOAD, isBuyNoAD);
	}

	public HashMap<String, StageData> getStageDatas() {
		synchronized (stageDatasSyncLock) {
			return stageDatas;
		}
	}

	public int getGoldCount() {
		return Integer.parseInt(goldCount.substring(1));

	}

	public void setGoldCount(int goldCount) {

		this.goldCount = "g" + goldCount;
		ShareSettingUtil.storeStringSetting(GOLD_COUNT_KEY, this.goldCount);
	}

	public int getLampCount() {
		return lampCount;
	}

	public void setLampCount(int lampCount) {
		this.lampCount = lampCount;
		ShareSettingUtil.storeIntSetting(LAMP_COUNT_KEY, lampCount);
	}

	public int getLastStage() {
		return lastStage;
	}

	public void setLastStage(int lastStage) {
		this.lastStage = lastStage;
		ShareSettingUtil.storeIntSetting(LAST_STAGE_KEY, lastStage);
	}

	/**
	 * 保存当前关卡存档
	 */
	public void saveCurrentStageData(String string) {
		synchronized (stageDatasSyncLock) {
			ShareSettingUtil.storeStringSetting(STATE_DATA_ONE_KEYPRE + string,
					GsonHelper.getJsonString(stageDatas.get(string)));
		}
	}

	public void initStageData() {
		mapInfos = new HashMap<String, MapInfo>();

		stageDatas = new HashMap<String, StageData>();

		String stage1_1Str = ShareSettingUtil
				.getStringSetting(STATE_DATA_ONE_KEYPRE + "1_1");
		if (stage1_1Str.equalsIgnoreCase("null")) {
			// 初始化关卡,解锁第一章和第二章的50个关卡
			ShareSettingUtil.storeBooleanSetting(CLASS_UNLOCK_KEY + 1, true);
			ShareSettingUtil.storeBooleanSetting(CLASS_UNLOCK_KEY + 2, true);
			int classInditify = 1;
			for (int i = 1; i <= GameSystem.stageCount; i++) {
				String key = classInditify + "_" + i;
				StageData sd = new StageData();
				int mapAllCount = Ccc.a(key);
				sd.setAllCount(mapAllCount);
				stageDatas.put(key, sd);
			}
			classInditify = 2;
			for (int i = 1; i <= GameSystem.stageCount; i++) {
				String key = classInditify + "_" + i;
				StageData sd = new StageData();
				int mapAllCount = Ccc.a(key);
				sd.setAllCount(mapAllCount);
				stageDatas.put(key, sd);
			}
			saveAllStages();
		} else {
			for (int classIndex = 1; classIndex <= GameSystem.CurrentClassOpenCount; classIndex++) {
				// 检查是否解锁
				// 初始化关卡,解锁第一章的50个关卡
				boolean isClassUnlocked = ShareSettingUtil.getBooleanSetting(
						CLASS_UNLOCK_KEY + classIndex, false);

				if (isClassUnlocked) {
					for (int i = 1; i <= GameSystem.stageCount; i++) {
						String key = classIndex + "_" + i;
						String temp = ShareSettingUtil
								.getStringSetting(STATE_DATA_ONE_KEYPRE + key);
						if (!temp.equalsIgnoreCase("null")) {
							StageData stageData = (StageData) GsonHelper
									.getObjectbyJsonString(temp,
											new TypeToken<StageData>() {
											}.getType());
							if (stageData.getAllCount() == 0) {
								int mapAllCount = Ccc.a(key);
								stageData.setAllCount(mapAllCount);
							}
							stageDatas.put(key, stageData);
						} else {
							// 如果没有数据，使用一个新数据代替
							StageData stageData = new StageData();
							int mapAllCount = Ccc.a(key);
							stageData.setAllCount(mapAllCount);
							stageDatas.put(key, stageData);
						}
					}
				}
			}
		}

		lastStage = ShareSettingUtil.getIntSetting(LAST_STAGE_KEY, 0);
		lampCount = ShareSettingUtil.getIntSetting(LAMP_COUNT_KEY, 2);
		lastGameClass = ShareSettingUtil.getIntSetting(LAST_GAME_CLASS_KEY, 0);
		lastClass = lastGameClass;
		// 启动获得10金币
		goldCount = ShareSettingUtil.getStringSetting(GOLD_COUNT_KEY, "g10");

		isBuyNoAD = ShareSettingUtil.getBooleanSetting(ISBUY_NOAD, false);
		if (isBuyNoAD) {
			GameSystem.GameMatrixStartY = 630;
		}

		isAudioOn = ShareSettingUtil.getBooleanSetting(IS_AUDIO_OPEN_KEY, true);
	}

	/**
	 * 保存所有关卡信息
	 */
	@SuppressWarnings("rawtypes")
	private void saveAllStages() {
		java.util.Iterator it = stageDatas.entrySet().iterator();
		while (it.hasNext()) {
			java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
			saveCurrentStageData((String) entry.getKey());
		}
	}

	/**
	 * 获取当前关卡完成情况
	 * 
	 * @return
	 */
	public StageData getCurrentStageData() {
		return stageDatas.get(lastClass + "_" + lastStage);
	}

	public long getMyScore() {
		long result = 0;
		for (int i = 1; i <= GameSystem.stageCount; i++) {
			if (StageManager.instance.stageDatas.containsKey(i)) {
				result += StageManager.instance.stageDatas.get(i).getScore();
			} else {
				break;
			}
		}
		return result;
	}

	public void clearCurrentStage() {
		String key = lastClass + "_" + lastStage;
		int wordsCount = stageDatas.get(key).getAllCount();
		StageData sd = new StageData();
		sd.setAllCount(wordsCount);
		stageDatas.put(key, sd);

	}

	/**
	 * 解锁关卡
	 * 
	 * @param classIndex
	 */
	public void unlockClassStatus(int classIndex) {
		ShareSettingUtil.storeBooleanSetting(CLASS_UNLOCK_KEY + classIndex,
				true);
	}

	/**
	 * 1-正常 2-锁定 3-不可用
	 * 
	 * @param classIndex
	 * @return
	 */
	public int getClassStatus(int classIndex) {
		if (classIndex <= GameSystem.CurrentClassOpenCount) {
			if (ShareSettingUtil.getBooleanSetting(CLASS_UNLOCK_KEY
					+ classIndex, false)) {
				return 1;
			} else {
				return 2;
			}
		} else {

			return 3;
		}
	}

	public void saveLastClassStageIndex(int cIndex, int sIndex) {
		ShareSettingUtil.storeIntSetting(LAST_CLASS_STAGE_INDEX + cIndex,
				sIndex);
	}

	public int getLastClassStageIndex(int cIndex) {
		return ShareSettingUtil.getIntSetting(LAST_CLASS_STAGE_INDEX + cIndex,
				1);
	}

	public synchronized boolean consumeGold(int price) {
		if (getGoldCount() >= price) {
			setGoldCount(getGoldCount() - price);
			return true;
		} else {
			return false;
		}

	}

	public synchronized boolean isEnoughGold(int price) {
		return getGoldCount() >= price;
	}

	public synchronized void unlockClassAllStage(int classIndex) {
		boolean isClassUnlocked = ShareSettingUtil.getBooleanSetting(
				CLASS_UNLOCK_KEY + classIndex, false);

		if (isClassUnlocked) {
			for (int i = 1; i <= GameSystem.stageCount; i++) {
				String key = classIndex + "_" + i;
				String temp = ShareSettingUtil
						.getStringSetting(STATE_DATA_ONE_KEYPRE + key);
				if (temp.equalsIgnoreCase("null")) {
					StageData stageData = new StageData();
					int mapAllCount = Ccc.a(key);
					stageData.setAllCount(mapAllCount);
					stageDatas.put(key, stageData);
				}
			}
			saveAllStages();
		}
	}
}
